﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using wi = System.Windows.Interactivity;
using System.ComponentModel;

namespace Thinktecture.UIAnnotations.Behaviors
{
    public class DialogButtonBehavior : Behavior<Button>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.Loaded += this.OnLoaded;
        }

        protected override void OnDetaching()
        {
            this.AssociatedObject.Loaded -= this.OnLoaded;
            base.OnDetaching();
        }

        #region OnLoaded
        
        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            var root = VisualTreeHelpers.GetRootElement(this.AssociatedObject);
            if (root != null && this.ButtonType != Thinktecture.UIAnnotations.Behaviors.ButtonType.None)
            {
                wi.TargetedTriggerAction action = null;
                if (this.ButtonType == Thinktecture.UIAnnotations.Behaviors.ButtonType.Cancel ||
                    this.ButtonType == Thinktecture.UIAnnotations.Behaviors.ButtonType.Close)
                {
                    action = new CancelButtonBehavior();
                }
                else if (this.ButtonType == Thinktecture.UIAnnotations.Behaviors.ButtonType.Ok)
                {
                    action = new DefaultButtonBehavior();
                }
                var trigger = new wi.EventTrigger("Loaded");
                action.TargetObject = this.AssociatedObject;
                trigger.Actions.Add(action);
                wi.Interaction.GetTriggers(root).Add(trigger);
            }

            // set the close window behavior on the button
            var closeTrigger = new wi.EventTrigger("Click");
            var closeAction = new CloseWindowBehavior() { DialogResult = this.ButtonType == Thinktecture.UIAnnotations.Behaviors.ButtonType.Ok };
            closeTrigger.Actions.Add(closeAction);
            wi.Interaction.GetTriggers(this.AssociatedObject).Add(closeTrigger);

            // set the content of the button
            var content = Enum.GetName(typeof(ButtonType), this.ButtonType);
            this.AssociatedObject.Content = content;
        }
        
        #endregion

        #region ButtonType

        public static readonly DependencyProperty ButtonTypeProperty =
            DependencyProperty.Register("ButtonType", typeof(ButtonType), typeof(DialogButtonBehavior), new PropertyMetadata(ButtonType.None));
        public ButtonType ButtonType { get { return (ButtonType)GetValue(ButtonTypeProperty); } set { SetValue(ButtonTypeProperty, value); } }

        #endregion

        #region Attach

        public static readonly DependencyProperty AttachProperty =
            DependencyProperty.RegisterAttached("Attach", typeof(ButtonType), typeof(DefaultButtonBehavior),
            new PropertyMetadata(ButtonType.None, OnAttachPropertyChanged));

        public static ButtonType GetAttach(UIElement element)
        {
            return (ButtonType)element.GetValue(AttachProperty);
        }

        public static void SetAttach(UIElement element, ButtonType value)
        {
            element.SetValue(AttachProperty, value);
        }

        private static void OnAttachPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(sender)) return;
            ButtonType buttonType;
            if (e.NewValue != null && 
                Enum.TryParse<ButtonType>(e.NewValue.ToString(), out buttonType) &&
                buttonType != ButtonType.None)
            {
                var behavior = new DialogButtonBehavior() { ButtonType = buttonType };
                wi.Interaction.GetBehaviors(sender).Add(behavior);
            }
        }

        #endregion

    }

    public enum ButtonType
    {
        None,
        Ok,
        Cancel,
        Close
    }
}
